//
//  ZDDComposePictureView.swift
//  小蜗牛_新浪微博
//
//  Created by 周冬冬 on 16/7/3.
//  Copyright © 2016年 我的mac. All rights reserved.
//

import UIKit
// cell 可重用标识符
private let pictureViewCellId = "pictureViewCellId"
class ZDDComposePictureView: UICollectionView {
    // 告知外界弹出相册
    var composePictureViewClosure:(()->())?
   
    
    // 保存image的数组
    var imageList:[UIImage] = [UIImage]()
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func addImages(image: UIImage){
    
        imageList.insert(image, atIndex: 0)
        if imageList.count > 9{
            hidden = false
        
            imageList.removeLast()
        }
    
    //刷新
        reloadData()
    }
    func setupUI(){
    
    backgroundColor = UIColor.whiteColor()
        hidden = true
    delegate = self
    dataSource = self
    registerClass(ZDDComposePictureViewCell.self, forCellWithReuseIdentifier: pictureViewCellId)
        //设置cell的布局
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        let itemMargin : CGFloat = 5
        let cellHW = (kScreenW - StautsMargin*2 - itemMargin*2)/3
        layout.itemSize = CGSize(width: cellHW, height: cellHW)
        //水平垂直间距
        layout.minimumInteritemSpacing = itemMargin
        layout.minimumLineSpacing = itemMargin
    
    }
    
}
extension ZDDComposePictureView:UICollectionViewDelegate,UICollectionViewDataSource{

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        let count = imageList.count
        if count == 0 || count == 9 {
        
            return count
        }else{
        return count + 1
        }
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(pictureViewCellId, forIndexPath: indexPath) as! ZDDComposePictureViewCell
        if imageList.count == indexPath.item {
        
       cell.image = nil
        }else{
        
            self.hidden = false
            cell.image = imageList[indexPath.item]
        
        }
        cell.deleteButtonCloure = {
        
            self.imageList.removeAtIndex(indexPath.item)
            
            if self.imageList.count == 0 {
            
                self.hidden = true
            
            }
            
            //刷新数据
            self.reloadData()
        }
        
        return cell
    }
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        if imageList.count == indexPath.item {
        
            composePictureViewClosure?()
        }
    }

}

class ZDDComposePictureViewCell:UICollectionViewCell{

    //声明删除闭包
    var deleteButtonCloure:(()->())?
    var image:UIImage?{
        didSet{
        
            if image == nil {
            
                bgImageView.image = UIImage(named: "compose_pic_add")
                bgImageView.highlightedImage = UIImage(named: "compose_pic_add_highlighted")
                deleteButton.hidden = true
            }else{
            
                bgImageView.image = image
                bgImageView.highlightedImage = nil
                deleteButton.hidden = false
            }
        
        }
        
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setupUI(){
    
        backgroundColor = UIColor.whiteColor()
    
        //添加控件
        contentView.addSubview(bgImageView)
        contentView.addSubview(deleteButton)
        //添加约束
        bgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        //删除按钮
        deleteButton.snp_makeConstraints { (make) -> Void in
            make.top.trailing.equalTo(contentView)
            
        }
    
    
    }
  @objc private func deleteButtonClick(){
    
    deleteButtonCloure?()
    

    }
    private lazy var bgImageView : UIImageView = UIImageView()
    //删除按钮
    private lazy var deleteButton : UIButton = {
    
        let button = UIButton()
        button.addTarget(self, action: "deleteButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        button.setImage(UIImage(named: "compose_photo_close"), forState: UIControlState.Normal)
        return button
    
    
    }()
}













